<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <!-- 搜索 -->
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="是否推送" class="row-padding-bottom">
              <el-select
                v-model="filterField.isPush"
                filterable
                placeholder="请选择是否推送"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in isPushArr"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="姓名" class="row-padding-bottom">
              <el-input
                placeholder="请输入姓名"
                clearable
                v-model="filterField.projectName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-form-item>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <!-- 表格 -->
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <el-button
            class="commonBut saveBtn funComBut"
            @click="startPush"
            :loading="synchronizeLoading"
          >
            <span class="opetareText">开启推送</span>
          </el-button>
        </div>
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
          :row-key="recordId"
          @select-all="selectAll"
          @select="selectionChange"
        >
          <el-table-column
            type="selection"
            width="80"
            :reserve-selection="true"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="isPushDesc"
            label="状态"
            :formatter="formatTd"
            min-width="180"
          ></el-table-column>
          <el-table-column label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="项目状态"
            :formatter="formatTd"
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="areaName"
            label="区域"
            :formatter="formatTd"
            min-width="160"
          ></el-table-column>
          <el-table-column label="施工方" :formatter="formatTd" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pbName"
                :columnName="'pbName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column label="建设方" :formatter="formatTd" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pcName"
                :columnName="'pcName'"
              ></copyText>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- 分页 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'pushConfigList',
  mixins: [scrollTable],
  data() {
    return {
      requestUrl: 'api-out/sz/project/config/list',
      //表格数据
      tableList: [],
      isShow: false, //列表加载
      loading: true,
      filterField: {
        isPush: '',
        projectName: '',
      },
      pageSize: 20,
      pageNo: 1,
      isPushArr: [
        {
          code: 'PUSH',
          name: '推送',
        },
        {
          code: 'UN_PUSH',
          name: '不推送',
        },
      ],
      titleText: '推送配置',
      checkedArr: [],
      checkedId: [],
      synchronizeLoading: false,
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  mounted() {},
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    recordId(row) {
      return row.id
    },
    selectionChange(rows, row) {
      var selected = rows.length && rows.indexOf(row) !== -1
      if (selected) {
        this.checkedArr = rows
      } else {
        this.checkedArr.forEach((item, i) => {
          if (item.id == row.id) {
            this.checkedArr.splice(i, 1)
          }
        })
      }
    },
    selectAll(selection) {
      this.checkedArr = selection
      if (selection) {
        selection.forEach((row) => {
          this.$refs.tableBox1.toggleRowSelection(row, true)
        })
      } else {
        this.$refs.tableBox1.clearSelection()
      }
    },
    startPush() {
      if (this.checkedArr.length > 0) {
        this.checkedArr.forEach((row) => {
          this.checkedId.push(row.id)
        })
      } else {
        this.$message({
          message: '请选择需要开启推送的项目',
          type: 'success',
        })
        return false
      }
      this.synchronizeLoading = true
      this.axios({
        method: 'post',
        url: 'api-out/sz/project/config/add',
        data: {
          projectIdArr: JSON.stringify(this.checkedId),
        },
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.$message({
                message: response.data.result,
                type: 'success',
              })
              this.checkedId = []
              this.checkedArr = []
              this.$refs.tableBox1.clearSelection()
              this.searchList()
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
            this.synchronizeLoading = false
          },
          (response) => {}
        )
        .catch((error) => {
          this.synchronizeLoading = true
        })
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableList = data
      // if(this.tableList.length > 0) {
      //     this.tableList.forEach((item, i) => {
      //         if(item.isPush == 'PUSH') {
      //             this.checkedArr.push(item);
      //         }
      //     });
      // }
      // if(this.checkedArr) {
      //     this.checkedArr.forEach(row => {
      //         this.$refs.tableBox1.toggleRowSelection(row, true)
      //     })
      // }
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
  },
}
</script>

<style lang="less" scoped>
.mainCon {
  top: 212px;
}
@import '../../../static/css/filterArea.less';
</style>
